
import pygame, random, sys

pygame.mixer.pre_init()
pygame.init()
screen = pygame.display.set_mode((576, 1024))
clock = pygame.time.Clock()

bg_surface = pygame.image.load('assets/background-day.png').convert()
bg_surface = pygame.transform.scale2x(bg_surface)

floor_surface = pygame.image.load('assets/base.png').convert()
floor_surface = pygame.transform.scale2x(floor_surface)
floor_x_pos = 0

game_active = True

def draw_floor():
  screen.blit(floor_surface, (floor_x_pos, 900))
  screen.blit(floor_surface, (floor_x_pos + 576, 900))

bird_downflap = pygame.image.load('assets/bluebird-downflap.png').convert()
bird_downflap = pygame.transform.scale2x(bird_downflap)
bird_midflap = pygame.image.load('assets/bluebird-midflap.png').convert()
bird_midflap = pygame.transform.scale2x(bird_midflap)
bird_upflap = pygame.image.load('assets/bluebird-upflap.png').convert()
bird_upflap = pygame.transform.scale2x(bird_upflap)

bird_frames = [bird_downflap, bird_midflap, bird_upflap]
bird_index = 0
bird_surface = bird_frames[bird_index]
bird_rect = bird_surface.get_rect(center = (100, 512))

BIRDFLAP = pygame.USEREVENT + 1
pygame.time.set_timer(BIRDFLAP, 200)

game_font = pygame.font.Font('fonts/04B_19.TTF', 40)

gravity = 0.25
bird_movement = 0

score = 0
high_score = 0

SPAWNPIPE = pygame.USEREVENT
pygame.time.set_timer(SPAWNPIPE, 1200)

pipe_surface = pygame.image.load('assets/pipe-green.png').convert()
pipe_surface = pygame.transform.scale2x(pipe_surface)

pipe_list = []
pipe_height = [400, 600, 800]
 
def create_pipe():
  random_pipe_y = random.choice(pipe_height)
  top_pipe = pipe_surface.get_rect(midbottom = (700, random_pipe_y - 300))
  bottom_pipe = pipe_surface.get_rect(midtop = (700, random_pipe_y))
  return top_pipe, bottom_pipe

def move_pipes(pipes):
  for pipe in pipes:
    pipe.centerx -= 5
    
  return pipes

def draw_pipes(pipes):
  for pipe in pipes:
    if pipe.bottom < 1024:
      # top pipe
      # False: flipX
      # True: flipY
      flip_pipe_surface = pygame.transform.flip(pipe_surface, False, True)
      screen.blit(flip_pipe_surface, pipe)
    else:
      screen.blit(pipe_surface, pipe)

def check_collision(pipes):
  for pipe in pipes:
    if bird_rect.colliderect(pipe):
      print("Collision!!")
      game_over_sound.play()
      return False
  
  if bird_rect.top <= -100 or bird_rect.bottom >= 900:
    print("Collision on edges!!")
    game_over_sound.play()
    return False
  
  return True

def rotate_bird(bird):
  new_bird = pygame.transform.rotozoom(bird, -bird_movement * 2, 1)
  return new_bird

def bird_animation():
  new_bird = bird_frames[bird_index]
  new_bird_rect = new_bird.get_rect(center = (100, bird_rect.centery))
  return new_bird, new_bird_rect

def score_display(is_game_over):
  score_surface = game_font.render(f'Score: {int(score)}', True, (255, 255, 255)) # True for antialias
  score_rect = score_surface.get_rect(center = (288, 100))

  screen.blit(score_surface, score_rect)

  if is_game_over:
    high_score_surface = game_font.render(f'High Score: {int(high_score)}', True, (255, 255, 255)) # True for antialias
    high_score_rect = high_score_surface.get_rect(center = (288, 850))

    screen.blit(high_score_surface, high_score_rect)

def pipe_score_check():
  global score

  if pipe_list:
    for pipe in pipe_list:
      if 95 < pipe.centerx < 105:
        score += 1
        score_sound.play()


game_over_surface = pygame.image.load('assets/message.png').convert_alpha()
game_over_surface = pygame.transform.scale2x(game_over_surface)
game_over_rect = game_over_surface.get_rect(center = (288, 512))

flap_sound = pygame.mixer.Sound('sound/sfx_wing.wav')
game_over_sound = pygame.mixer.Sound('sound/sfx_hit.wav')
score_sound = pygame.mixer.Sound('sound/sfx_point.wav')
countdown = 100

while True:
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      pygame.quit()
      sys.exit()
      
    if event.type == pygame.KEYDOWN:
      if event.key == pygame.K_SPACE and game_active:
        bird_movement = 0
        bird_movement -= 10

        flap_sound.play()
      if event.key == pygame.K_SPACE and game_active == False:
        pipe_list.clear()
        bird_movement = 0
        score = 0
        bird_rect.center = (100, 512)
        game_active = True
        countdown = 100
      
    if event.type == SPAWNPIPE:
      pipe_list.extend(create_pipe())

    if event.type == BIRDFLAP:
      bird_index += 1
      bird_index = bird_index % 3
      bird_surface, bird_rect = bird_animation()
      
  screen.blit(bg_surface, (0, 0))
  
  if game_active:
    bird_movement += gravity
    bird_rect.centery += bird_movement

    rotated_bird = rotate_bird(bird_surface)

    screen.blit(rotated_bird, bird_rect)
    game_active = check_collision(pipe_list)
    
    pipe_list = move_pipes(pipe_list)
    draw_pipes(pipe_list)

    # score += 0.01
    
    # countdown -= 1
    # if countdown <= 0:
    #   score_sound.play()
    #   countdown = 100

    pipe_score_check()

    score_display(False)
  else:
    screen.blit(game_over_surface, game_over_rect)
    high_score = max(high_score, score)
    score_display(True)
  
  floor_x_pos -= 1
  draw_floor()
  if floor_x_pos <= -576:
    floor_x_pos = 0
    
    
  pygame.display.update()
  clock.tick(120)